package net.onewfour.utils;

import java.security.MessageDigest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class OneW4Utils {
	/**
	 * MD5加密
	 * 
	 * @user Marc
	 * @createtime 2014-6-4 下午02:52:59
	 * @param key
	 * @return
	 */
	public static String MD5(String key) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'A', 'B', 'C', 'D', 'E', 'F' };
		try {
			byte[] btInput = key.getBytes();
			// 获得MD5摘要算法的 MessageDigest 对象
			MessageDigest mdInst = MessageDigest.getInstance("MD5");
			// 使用指定的字节更新摘要
			mdInst.update(btInput);
			// 获得密文
			byte[] md = mdInst.digest();
			// 把密文转换成十六进制的字符串形式
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			return null;
		}
	}
	
	
	public static String getTime() {
		return getTime("yyyy-MM-dd hh:mm:ss");
	}

	public static String convertTime(String s) {
		return convertTime(s, "yyyyMMddhhmmss", "yyyy-MM-dd hh:mm:ss");
	}

	public static String convertTime(String s, String fsdf, String tsdf) {
		try {
			Date date = new SimpleDateFormat(fsdf).parse(s);
			SimpleDateFormat sdf = new SimpleDateFormat(tsdf);
			return sdf.format(date).toString();
		} catch (ParseException e) {
		}
		return "";
	}

	/**
	 * 获取时间
	 * 
	 * @user Marc
	 * @createtime 2014-6-23 下午05:22:00
	 * @return
	 */
	public static String getTime(String gs) {
		Date dt = Calendar.getInstance(TimeZone.getTimeZone("GMT+8")).getTime();
		// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		SimpleDateFormat sdf = new SimpleDateFormat(gs);
		return sdf.format(dt).toString();
	}
}
